<?php
/**
 * Celebrio Kernel
 *
 * @copyright  Copyright (c) 2012 Celebio Software, s.r.o.
 * @package    Kernel
 */
namespace KernelModule\LogsModule\LoggerModule;

use Nette\Object;
use KernelModule\LogsModule\LoggerModule\ILogReader;
use Celebrio\WindowsAzure\AzureStorage\AzureStorageTable;

/**
 * Interface for reading logs in Celebrio
 * 
 * @author Martin Novák
 */
class LogReaderAzure extends Object implements ILogReader {
    
	/**
	 * returns array with stored logs
	 * array(
	 *	  "id" => mixed,
	 *	  "time" => date("D, d M Y H:i:s"),
	 *	  "level" => string,
	 *	  "file" => string,
	 *	  "class" => string,
	 *	  "method" => string,
	 *	  "line" => int,
	 *	  "message" => string)
	 * @param string $target name of target table or file
	 * @param mixed $from start reading from
	 * @return array
	 */
    public static function getLogsList($target,$from = NULL) {
    	$results = array();

    	if(AzureStorageTable::connection()->tableExists($target)) {
    		if ($from == NULL || (is_array($from) && empty($from[0])) ){
	    		$requests = AzureStorageTable::connection()->retrieveEntities($target);
    		} else {
    			$requests = AzureStorageTable::connection()->retrieveEntities($target,"","Microsoft_WindowsAzure_Storage_DynamicTableEntity",$from[0],$from[1]);
    		}
	    	$i = 0;
	    	foreach($requests as $request){
	    		$results[] = array(
	    				"id" => array($request->PartitionKey,$request->RowKey),
	    				"time" => date("D, d M Y H:i:s",$request->RowKey),
	    				"level" => $request->level,
	    				"file" => $request->fileName,
	    				"class" => $request->className,
	    				"method" => $request->method,
	    				"line" => $request->lineNumber,
	    				"message" => $request->message);
	    		$i++;
	    		if($i==50) break;
	    	}
    	}
    	return $results;
    }

}